package com.realife.eson.common.util.information;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;


@Slf4j
public class MeLinuxUtil {

    private MeLinuxUtil() {
    }


    /**
     *
     * @param command shell脚本
     * @return
     * @throws IOException
     */
    public static List<String> executeCommand(String command) throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("/bin/sh","-c",command);
        Process process = processBuilder.start();
        process.getOutputStream().close();
        List<String> result = Lists.newArrayList();
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while (input.read() != -1){
            result.add(input.readLine());
        }
        BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while (err.read() != -1){
            result.add(err.readLine());
        }
        input.close();
        err.close();
        process.destroyForcibly();
        return result;
    }



}

